package cn.xiehangs;

public  class qi_pan_wz {
    static int[]wz=new int[100];

    static qi_zi_group group0;
    static qi_zi_group group1;
    static qi_zi_group group2;
    static qi_zi_group group3;


    public qi_pan_wz(qi_zi_group group0, qi_zi_group group1, qi_zi_group group2, qi_zi_group group3){
        for (int i=0;i<100;i++){
            wz[i]=-1;//初始化
        }
        this.group0=group0;
        this.group1=group1;
        this.group2=group2;
        this.group3=group3;
    }

    public static void pan_daun(int id, int step){
        if(step>50||step<=0){
            return;
        }
        int true_step = 0;//棋盘上的真实位置
        switch (id/4){
            case 0:
                true_step=step;
                break;
            case 1:
                true_step=step+13;
                break;
            case 2:
                true_step=step+26;
                break;
            case 3:
                true_step=step+39;
                break;
        }
        if(true_step>52){
            true_step-=52;
        }
        if(wz[true_step]==-1){
            wz[true_step]=10000*(id/4);
        int set=1;
        for (int i=1;i<(id%4+1);i++) {
             set*=10;
        }
        wz[true_step]+=set;
        }
        else if((wz[true_step]/10000)==(id/4)){
            int set=1;
            for (int i=1;i<(id%4+1);i++) {
                set*=10;
            }
            wz[true_step]+=set;
        }
        else {
            clear(true_step);
            wz[true_step]=10000*(id/4);
            int set=1;
            for (int i=1;i<(id%4+1);i++) {
                set*=10;
            }
            wz[true_step]+=set;
        }
    }

    public static void leave(int id, int step){
        if (step>50||step<=0){
            return;
        }
        int true_step = 0;//棋盘上的真实位置
        switch (id/4){
            case 0:
                true_step=step;
                break;
            case 1:
                true_step=step+13;
                break;
            case 2:
                true_step=step+26;
                break;
            case 3:
                true_step=step+39;
                break;
        }
        if(true_step>52){
            true_step-=52;
        }
        int set=1;
        for (int i=1;i<(id%4+1);i++) {
            set*=10;
        }
        wz[true_step]-=set;
        Boolean flag=false;
        int temp=wz[true_step];
        for (int i=0;i<4;i++){
            if (temp%10!=0){
                flag=true;
                break;
            }
            temp/=10;
        }
        if(flag==false){
            wz[true_step]=-1;
        }
    }

    public static void clear(int true_step){
        int temp=wz[true_step];
        boolean[]flag={false,false,false,false};
        for (int i=0;i<4;i++){
            if (temp%10!=0){
                flag[i]=true;
            }
            temp/=10;
        }
        switch (wz[true_step]/10000){
            case 0:
                for (int i=0;i<4;i++){
                    if (flag[i]){
                        switch (i){
                            case 0:
                                group0.number_1.back();
                                break;
                            case 1:
                                group0.number_2.back();
                                break;
                            case 2:
                                group0.number_3.back();
                                break;
                            case 3:
                                group0.number_4.back();
                                break;
                        }
                    }
                }
                break;
            case 1:
                for (int i=0;i<4;i++){
                    if (flag[i]){
                        switch (i){
                            case 0:
                                group1.number_1.back();
                                break;
                            case 1:
                                group1.number_2.back();
                                break;
                            case 2:
                                group1.number_3.back();
                                break;
                            case 3:
                                group1.number_4.back();
                                break;
                        }
                    }
                }
                break;
            case 2:
                for (int i=0;i<4;i++){
                    if (flag[i]){
                        switch (i){
                            case 0:
                                group2.number_1.back();
                                break;
                            case 1:
                                group2.number_2.back();
                                break;
                            case 2:
                                group2.number_3.back();
                                break;
                            case 3:
                                group2.number_4.back();
                                break;
                        }
                    }
                }
                break;
            case 3:
                for (int i=0;i<4;i++){
                    if (flag[i]){
                        switch (i){
                            case 0:
                                group3.number_1.back();
                                break;
                            case 1:
                                group3.number_2.back();
                                break;
                            case 2:
                                group3.number_3.back();
                                break;
                            case 3:
                                group3.number_4.back();
                                break;
                        }
                    }
                }
                break;
        }
    }

}
